#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from bs4 import BeautifulSoup


def getIdentifiers(identifiers):
    result = {}
    primary_id = identifiers.PRIMARY_ID
    if primary_id != None:
        result["PRIMARY_ID"] = getNameAndLabel(primary_id)
    secondary_ids = identifiers.find_all('SECONDARY_ID')
    if len(secondary_ids) > 0:
        result["SECONDARY_ID"] = []
        for secondary_id in secondary_ids:
            result["SECONDARY_ID"].append(getNameAndLabel(secondary_id))
    external_ids = identifiers.find_all('EXTERNAL_ID')
    if len(external_ids) > 0:
        result["EXTERNAL_ID"] = []
        for external_id in external_ids:
            result["EXTERNAL_ID"].append(getNameAndLabel(external_id))
    submitter_ids = identifiers.find_all('SUBMITTER_ID')
    if len(submitter_ids) > 0:
        result["SUBMITTER_ID"] = []
        for submitter_id in submitter_ids:
            result["SUBMITTER_ID"].append(getNameAndLabel(submitter_id))
    uuid_ids = identifiers.find_all('UUID')
    if len(uuid_ids) > 0:
        result["UUID"] = []
        for uuid_id in uuid_ids:
            result["UUID"].append(getNameAndLabel(uuid_id))
    return result


def getNameAndLabel(nameandlabel):
    result = {}
    id = nameandlabel.string
    db = nameandlabel.get('namespace')
    attr = nameandlabel.get('label')
    if id != None:
        result['id'] = id
    if db != None:
        result['db'] = db
    if attr != None:
        result['attr'] = attr
    return result
